package com.lwlk.zdk.controller.Assess;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwlk.zdk.enums.RetCode;
import com.lwlk.zdk.mapper.pw.*;
import com.lwlk.zdk.mapper.task.pwTaskEnterpriseAssessmentStatisticsMapper;
import com.lwlk.zdk.mapper.task.pwTaskPlatformAssessmentStatisticsMapper;
import com.lwlk.zdk.mapper.task.pwTaskRegionAssessmentStatisticsMapper;
import com.lwlk.zdk.mapper.wd.wdHfAreaMapper;
import com.lwlk.zdk.model.pw.pwPlatformDayAssess;
import com.lwlk.zdk.model.pw.pwVehicleNetIn;
import com.lwlk.zdk.model.task.pwTaskEnterpriseAssessmentStatistics;
import com.lwlk.zdk.model.task.pwTaskPlatformAssessmentStatistics;
import com.lwlk.zdk.model.task.pwTaskRegionAssessmentStatistics;
import com.lwlk.zdk.model.wd.wdHfArea;
import com.lwlk.zdk.util.HttpClientUtils;
import com.lwlk.zdk.util.LayuiTablePagingResult;
import com.lwlk.zdk.util.PropUtils;
import com.lwlk.zdk.util.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.management.modelmbean.RequiredModelMBean;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.lang.reflect.MalformedParameterizedTypeException;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "考核统计")
@RestController
@Transactional
@RequestMapping(value = "/platform")
public class PlatformController {

    @Autowired
    pwVehicleNetInMapper pwVehicleNetInMapper;
    @Autowired
    pwPlatformDayAssessMapper pwPlatformDayAssessMapper;
    @Autowired
    pwPlatformWeekAssessMapper pwPlatformWeekAssessMapper;
    @Autowired
    pwPlatformMonthAssessMapper pwPlatformMonthAssessMapper;
    @Autowired
    pwAssessGradeMapper pwAssessGradeMapper;
    @Autowired
    pwTaskRegionAssessmentStatisticsMapper ptrasMapper;
    @Autowired
    pwTaskPlatformAssessmentStatisticsMapper ptpsMapper;
    @Autowired
    pwTaskEnterpriseAssessmentStatisticsMapper ptesMapper;

    private final static String port = PropUtils.getString("ports");
    private final static String Ip = PropUtils.getString("Ip");
    private final static String clientCode = PropUtils.getString("clientCode");


    @ApiOperation(value = "平台考核统计入网率展示")
    @RequestMapping(value = "/showNetIn",method = RequestMethod.POST)
    public Response getShowNetIn(){
        Response response=new Response();
        try {
            List<pwVehicleNetIn> pwVehicleNetInList=pwVehicleNetInMapper.selectAll();
            response.setData(pwVehicleNetInList);
            response.setCode(RetCode.SUCCESS);
            response.setMsg("执行成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return response;
    }



    @ApiOperation(value = "平台考核查岗率展示")
    @RequestMapping(value = "/showPost",method = RequestMethod.POST)
    public Response getShowPost(){
     Response response=new Response();
     try {
         List<pwPlatformDayAssess>pwPlatformDayAssessList=pwPlatformDayAssessMapper.selectAll();
         List<pwPlatformDayAssess>pwPlatformWeekAssessList=pwPlatformWeekAssessMapper.selectAll();
         List<pwPlatformDayAssess>pwPlatformMonthAssessList=pwPlatformMonthAssessMapper.selectAll();
         HashMap map=new HashMap();
         map.put("查岗日考核",pwPlatformDayAssessList);
         map.put("查岗周考核",pwPlatformWeekAssessList);
         map.put("查岗月考核",pwPlatformMonthAssessList);
         response.setData(map);
         response.setCode(RetCode.SUCCESS);
         response.setMsg("执行成功");
     }catch (Exception e){
         e.printStackTrace();
     }
     return response;
    }


    @ApiOperation(value = "市县级考核统计查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "page",value = "条数",paramType = "query",required=true),
            @ApiImplicitParam(name = "size",value = "页数",paramType = "query",required=true),
            @ApiImplicitParam(name = "TheInspectionTime",value = "考核时间",paramType = "query",required=true),
            @ApiImplicitParam(name = "assessGradeId",value = "考核方案",paramType = "query",required=true),
            @ApiImplicitParam(name = "status",value = "1:成功2:失败",paramType = "query"),
            @ApiImplicitParam(name = "type",value = "考核周期((0:日考核1:周考核2:月考核3:年考核)",paramType = "query",required=true),
            @ApiImplicitParam(name = "regionCode",value = "区域码",paramType = "query",required=true),
            @ApiImplicitParam(name = "level",value = "等级",paramType = "query",required = true)})
    @RequestMapping(value = "/CityAndCountyStatisticsShow",method = RequestMethod.GET)
    public LayuiTablePagingResult getCityAndCountyStatisticsShow(Integer page, Integer size,  pwTaskRegionAssessmentStatistics ps){
        LayuiTablePagingResult result=null;
        if (ps.getAssessGradeId()==null  || ps.getTheInspectionTime()==null){
            result=new LayuiTablePagingResult();
            result.setCode("500");
            result.setMsg("查询条件不可为空");
            return result;
        }
        PageHelper.startPage(page,size);
        String areaCode=null;
        if (ps.getLevel()==1){
            areaCode= String.valueOf(ps.getRegionCode()).substring(0,2);
        }else if (ps.getLevel()==2){
            areaCode= String.valueOf(ps.getRegionCode()).substring(0,4);
        }else if (ps.getLevel()==3){
            areaCode= ps.getRegionCode();
        }
        if (ps.getStatus()==null){
            ps.setStatus(1);
        }
        List<Map<String,Object>>dimList=ptrasMapper.selectDim(ps.getType(),ps.getTheInspectionTime(),ps.getAssessGradeId(),ps.getStatus(),areaCode);
        PageInfo pageInfo=new PageInfo<>(dimList);
        result = new LayuiTablePagingResult();
        result.setCode(RetCode.SUCCESS.getCode());
        result.setMsg(RetCode.SUCCESS.getMsg());
        result.setCount(pageInfo.getTotal());
        result.setData(pageInfo.getList());
        Long totalPages = pageInfo.getTotal() / size;
        if (pageInfo.getTotal() % size != 0){
            totalPages ++;
        }
        result.setTotalPages(totalPages);
        return result;
    }

    @ApiOperation(value = "平台考核统计查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "page",value = "条数",paramType = "query",required=true),
            @ApiImplicitParam(name = "size",value = "页数",paramType = "query",required=true),
            @ApiImplicitParam(name = "TheInspectionTime",value = "考核时间",paramType = "query",required=true),
            @ApiImplicitParam(name = "assessGradeId",value = "考核方案",paramType = "query",required=true),
            @ApiImplicitParam(name = "status",value = "1:成功2:失败",paramType = "query"),
            @ApiImplicitParam(name = "type",value = "考核周期((0:日考核1:周考核2:月考核3:年考核)",paramType = "query",required=true),
            @ApiImplicitParam(name = "regionCode",value = "区域码",paramType = "query",required=true),
            @ApiImplicitParam(name = "level",value = "等级",paramType = "query",required = true)})
    @RequestMapping(value = "/PlatformAssessmentShow",method = RequestMethod.GET)
    public LayuiTablePagingResult getPlatformAssessmentShow(Integer page, Integer size, @NotNull(message = "考核周期不可为空")Integer type, @NotNull(message = "考核时间不可为空") String TheInspectionTime,@NotNull(message = "考核方案不可为空") Integer assessGradeId, Integer status, @NotNull(message = "区域码不可为空")String regionCode, @NotNull(message = "等级不可为空")Integer level){
        LayuiTablePagingResult result=null;
        if (TheInspectionTime==null  || assessGradeId==null){
            result=new LayuiTablePagingResult();
            result.setCode("500");
            result.setMsg("查询条件不可为空");
            return result;
        }
        PageHelper.startPage(page,size);
        String areaCode=null;
        if (level==1){
            areaCode= String.valueOf(regionCode).substring(0,2);
        }else if (level==2){
            areaCode= String.valueOf(regionCode).substring(0,4);
        }else if (level==3){
            areaCode= regionCode;
        }
        if (status==null){
            status=1;
        }
        List<pwTaskPlatformAssessmentStatistics>dimList=ptpsMapper.selectDim(type,TheInspectionTime,assessGradeId,status);
        PageInfo<pwTaskPlatformAssessmentStatistics> pageInfo=new PageInfo<>(dimList);
        result = new LayuiTablePagingResult();
        result.setCode(RetCode.SUCCESS.getCode());
        result.setMsg(RetCode.SUCCESS.getMsg());
        result.setCount(pageInfo.getTotal());
        result.setData(pageInfo.getList());
        Long totalPages = pageInfo.getTotal() / size;
        if (pageInfo.getTotal() % size != 0){
            totalPages ++;
        }
        result.setTotalPages(totalPages);
        return result;
    }

    @ApiOperation(value = "企业考核统计查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "page",value = "条数",paramType = "query",required=true),
            @ApiImplicitParam(name = "size",value = "页数",paramType = "query",required=true),
            @ApiImplicitParam(name = "TheInspectionTime",value = "考核时间",paramType = "query",required=true),
            @ApiImplicitParam(name = "assessGradeId",value = "考核方案",paramType = "query",required=true),
            @ApiImplicitParam(name = "status",value = "1:成功2:失败",paramType = "query"),
            @ApiImplicitParam(name = "type",value = "考核周期((0:日考核1:周考核2:月考核3:年考核)",paramType = "query",required=true),
            @ApiImplicitParam(name = "regionCode",value = "区域码",paramType = "query",required=true),
            @ApiImplicitParam(name = "level",value = "等级",paramType = "query",required = true)})
    @RequestMapping(value = "/EnterpriseAssessmentShow",method = RequestMethod.GET)
    public LayuiTablePagingResult getEnterpriseAssessmentShow(Integer page, Integer size, @NotNull(message = "考核周期不可为空")Integer type, @NotNull(message = "考核时间不可为空") String TheInspectionTime,@NotNull(message = "考核方案不可为空") Integer assessGradeId, Integer status, @NotNull(message = "区域码不可为空")String regionCode, @NotNull(message = "等级不可为空")Integer level){
        LayuiTablePagingResult result=null;
        if (TheInspectionTime==null  || assessGradeId==null){
            result=new LayuiTablePagingResult();
            result.setCode("500");
            result.setMsg("查询条件不可为空");
            return result;
        }
        PageHelper.startPage(page,size);
        String areaCode=null;
        if (level==1){
            areaCode= String.valueOf(regionCode).substring(0,2);
        }else if (level==2){
            areaCode= String.valueOf(regionCode).substring(0,4);
        }else if (level==3){
            areaCode= regionCode;
        }
        if (status==null){
            status=1;
        }
        List<pwTaskEnterpriseAssessmentStatistics>dimList=ptesMapper.selectDim(type,TheInspectionTime,assessGradeId,status);
        PageInfo<pwTaskEnterpriseAssessmentStatistics> pageInfo=new PageInfo<>(dimList);
        result = new LayuiTablePagingResult();
        result.setCode(RetCode.SUCCESS.getCode());
        result.setMsg(RetCode.SUCCESS.getMsg());
        result.setCount(pageInfo.getTotal());
        result.setData(pageInfo.getList());
        Long totalPages = pageInfo.getTotal() / size;
        if (pageInfo.getTotal() % size != 0){
            totalPages ++;
        }
        result.setTotalPages(totalPages);
        return result;
    }


}
